package engine.utils
{
	import engine.classes.display.BitmapCreator;
	import engine.classes.display.BitmapWrapper;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;

	public class BitmapUtils
	{
		private static const creator:BitmapCreator = new BitmapCreator();
		
		public static function toBitmapData(display:DisplayObject, quality:Number = 1, transparent:Boolean = true):BitmapWrapper
		{
			creator.setTo(display, quality);
			
			var wrapper:BitmapWrapper 	= new BitmapWrapper();
			wrapper.display 			= display;
			wrapper.data 				= creator.createBitmapData(transparent);
			
			return wrapper;
		}
		
		public static function toBitmap(display:DisplayObject, quality:Number = 1, transparent:Boolean = true, smoothing:Boolean = true):BitmapWrapper
		{
			creator.setTo(display, quality);
			
			var wrapper:BitmapWrapper 	= new BitmapWrapper();
			wrapper.display 			= display;
			wrapper.bitmap				= creator.createBitmap(null, transparent, smoothing, true);
			wrapper.data 				= wrapper.bitmap.bitmapData;
			
			wrapper.bitmap.name = display.name;
			DisplayUtils.swapChild(display, wrapper.bitmap);
			
			return wrapper;
		}
		
		public static function toBitmapSprite(display:DisplayObject, quality:Number = 1, transparent:Boolean = true, smoothing:Boolean = true):BitmapWrapper
		{
			creator.setTo(display, quality);
			
			var wrapper:BitmapWrapper 	= new BitmapWrapper();
			wrapper.display 			= display;
			wrapper.sprite				= creator.createBitmapSprite(null, transparent, smoothing);
			wrapper.bitmap				= wrapper.sprite.getChildAt(0) as Bitmap;
			wrapper.data				= wrapper.bitmap.bitmapData;
			
			wrapper.sprite.name = display.name;
			DisplayUtils.swapChild(display, wrapper.sprite);
			
			return wrapper;
		}
		
		public static function equalBitmapData(data1:BitmapData, data2:BitmapData, transparent:Boolean = true):Boolean
		{
			if(data1.width != data2.width || data1.height != data2.height) return false;
			
			var width:int 	= data1.width;
			var height:int 	= data1.height;
			var x:int = 0;
			var y:int = 0;
			
			if(transparent)
			{
				for(x; x < width; x++)
				{
					for(y; y < height; y++)
					{
						if(data1.getPixel32(x, y) != data2.getPixel32(x, y)) return false;	
					}
				}
			}
			else
			{
				for(x; x < width; x++)
				{
					for(y; y < height; y++)
					{
						if(data1.getPixel(x, y) != data2.getPixel(x, y)) return false;	
					}
				}
			}
			
			return true;
		}
	}
}